﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget.Config
{
    [ConfigurationCollection(typeof(FavoriteElement),
    CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
    public class FavoriteElementCollection : ConfigurationElementCollection
    {
        #region Constructors
        static FavoriteElementCollection()
        {
            m_properties = new ConfigurationPropertyCollection();
        }

        public FavoriteElementCollection() { }
        #endregion

        #region Fields
        private static ConfigurationPropertyCollection m_properties;
        #endregion

        #region Properties
        protected override ConfigurationPropertyCollection Properties
        {
            get { return m_properties; }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
        }
        #endregion

        #region Indexers
        public FavoriteElement this[int index]
        {
            get { return (FavoriteElement)base.BaseGet(index); }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }

        public FavoriteElement GetByOrderKey(int orderKey)
        {
            foreach (FavoriteElement curFav in this)
            {
                if (curFav.OrderKey == orderKey)
                {
                    return curFav;
                }
            }

            return null;
        }

        #endregion

        #region Overrides
        protected override ConfigurationElement CreateNewElement()
        {
            return new FavoriteElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as FavoriteElement).OrderKey;
        }
        #endregion

        #region Methods

        public void Add(FavoriteElement favorite)
        {
            base.BaseAdd(favorite);
        }

        public void Remove(string contactID)
        {
            base.BaseRemove(contactID);
        }

        public void Remove(int orderKey)
        {
            base.BaseRemove(orderKey);
        }

        public void Remove(FavoriteElement favorite)
        {
            base.BaseRemove(GetElementKey(favorite));
        }

        public void Clear()
        {
            base.BaseClear();
        }

        public void RemoveAt(int index)
        {
            base.BaseRemoveAt(index);
        }

        public string GetKey(int index)
        {
            return (string)base.BaseGetKey(index);
        }
        #endregion
    }
}
